package com.dd.keel.core.database;

import java.sql.Connection;
import java.util.Map;
import java.util.Properties;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dd.keel.core.common.utils.RandomUtil;

/**
 * 不支持事务的DBManager实现
 * @author Kevin.XU
 *
 */
public class SimpleDatabaseService implements DBManager {

	final static private Logger LOGGER = LoggerFactory.getLogger(SimpleDatabaseService.class);
	
	private MutilDatasource mutilDatasource = new MutilDatasource();
	
	public void setDatasourceConfigs(Map<String, Properties> datasourceConfigs) {
		mutilDatasource.setDatasourceConfigs(datasourceConfigs);
	}
	
	public void init() {
	}

	public void fini() {
		mutilDatasource.destroy();
	}

	/**
	 * 直接返回一个JDBC连接
	 */
	public Connection getConnection(String key) throws Exception {
		Connection conn = null;
		try {
			conn = mutilDatasource.getDataSource(key).getConnection();
		} catch (Exception ex) {
			LOGGER.warn("Get Connectionn error for [" + key + "] : " + ex.getMessage());
		}
		return conn;
	}

	/**
	 * 返回一个数据源
	 */
	public DataSource getDataSource(String key) throws Exception {
		DataSource dataSource = null;
		try {
			dataSource = mutilDatasource.getDataSource(key);
		} catch (Exception ex) {
			LOGGER.warn("Get Datasource error for [" + key + "] : " + ex.getMessage());
		}
		return dataSource;
	}
	
}
